package cn.pinming.chyn.tools.utils;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import cn.pinming.chyn.tools.annotations.ValidateField;
import cn.pinming.chyn.tools.exception.ChynException;
import cn.pinming.chyn.tools.exception.ChynResultCode;
import cn.pinming.chyn.tools.test.EntityB;

public class ValidateUtil {

	public static void test(Object o) {
		
		List<Field> fields = new ArrayList<Field>() ;
		Field fieldArr[] = o.getClass().getDeclaredFields() ;
		fields.addAll(Arrays.asList(fieldArr)) ;
		
		Class superClass  = o.getClass().getSuperclass() ;
		while(superClass != null) {
			Field superFieldArr[] = superClass.getDeclaredFields() ;
			fields.addAll(Arrays.asList(superFieldArr)) ;
			superClass  = superClass.getSuperclass() ;
		}
		
		for(Field f : fields) {
			ValidateField validateField = f.getAnnotation(ValidateField.class) ;
			if(validateField == null) {
				continue ;
			}
			
			f.setAccessible(true);
			Object value = null ;
			try {
				value = f.get(o) ;
			} catch (Exception e) {
				e.printStackTrace();
			} 
			
			// 校验必填项
			if(!validateField.notNull()) {
				continue ;
			}
			if(value == null) {
				throw new ChynException(ChynResultCode.NO_PARAM, "属性"+f.getName()+"为必填项") ;
			}
			
			if(f.getType().equals(String.class)) {
				String valueString = String.valueOf(value) ;
				// 如果设置了固定长度 进行校验
				int length = validateField.length() ;
				if(length > 0 && valueString.length() != length) {
					throw new ChynException(ChynResultCode.STRING_ILLEGAL_LENGTH, "属性"+f.getName()+"固定长度为" + length) ;
				}
				
				// 检验字符长度
				int minLength = validateField.minLength() ;
				int maxLength = validateField.maxLength() ;
				if(minLength > valueString.length() || maxLength < valueString.length()) {
					throw new ChynException(ChynResultCode.STRING_ILLEGAL_LENGTH, "属性"+f.getName()+"长度限制为" + minLength +"~" + maxLength) ;
				}
				
				// 检验正则
				String regexPattern = validateField.regexPattern() ;
				if(!"".equals(regexPattern)) {
					Pattern pattern = Pattern.compile(regexPattern);
			        Matcher matcher = pattern.matcher(valueString);
			        
			        if (!matcher.matches()) {
			        	throw new ChynException(ChynResultCode.STRING_ILLEGAL_REGEX, "属性"+f.getName()+"正则校验失败"+regexPattern) ;
			        } 
				}
			}
			
			if(f.getType().equals(Integer.class)) {
				int valueInt =  Integer.valueOf(String.valueOf(value)) ;
				int min = validateField.min() ;
				int max = validateField.max() ;
				
				if(min > valueInt || max < valueInt) {
					throw new ChynException(ChynResultCode.NUMBER_ILLEGAL_RANGE, "属性"+f.getName()+"数字范围为" + min +"~" + max) ;
				}
			}
			
			if(f.getType().equals(Long.class)) {
				long valueLong =  Long.valueOf(String.valueOf(value)) ;
				long min = validateField.min() ;
				long max = validateField.max() ;
				
				if(min > valueLong || max < valueLong) {
					throw new ChynException(ChynResultCode.NUMBER_ILLEGAL_RANGE, "属性"+f.getName()+"数字范围为" + min +"~" + max) ;
				}
			}
			
		}
		
	}
	
	
	public static void main(String[] args) {
		
		EntityB entityB = new EntityB() ;
		
		entityB.setFieldB2("22-333-4444");
		entityB.setFieldB3("1234567890");
		entityB.setFieldB4(22);
		entityB.setFieldB5(44L);
		
		entityB.setFieldA2("22-333-4444");
		entityB.setFieldA3("1234567890");
		entityB.setFieldA4(22);
		entityB.setFieldA5(44L);
		
		ValidateUtil.test(entityB);
	}
	
	
}
